<?php
/**
* 关于匿名对象的空间占用，使用完即销毁
*/

$str = 'http://baidu.com';

$baseMemory = memory_get_usage();
$a = new Url64();
for ($i=0;$i<1000;$i++) {
    (new Url64())->urlsafeB64Encode($str);  // 匿名对象用完,直接销毁了
    //$a->urlsafeB64Encode($str);
}

echo memory_get_usage()-$baseMemory, "\n";


class Url64
{
	function __construct()
	{
		echo 'contruct' .PHP_EOL;
	}

	function __destruct()
	{
		echo 'destruct' .PHP_EOL;
	}

	/**
	 * Encode a string with URL-safe Base64.
	 *
	 * @param string $input The string you want encoded
	 *
	 * @return string The base64 encode of what you passed in
	 */
	public static function urlsafeB64Encode($input)
	{
	    return str_replace('=', '', strtr(base64_encode($input), '+/', '-_'));
	}

	/**
	 * Decode a string with URL-safe Base64.
	 *
	 * @param string $input A Base64 encoded string
	 *
	 * @return string A decoded string
	 */
	public static function urlsafeB64Decode($input)
	{
	    $remainder = strlen($input) % 4;
	    if ($remainder) {
	        $padlen = 4 - $remainder;
	        $input .= str_repeat('=', $padlen);
	    }
	    return base64_decode(strtr($input, '-_', '+/'));
	}
}